﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Chimp.Core;

namespace SpaceChimpIDE
{
    public partial class CodeComplete : Form
    {
        public string ActiveKey { get; set; }
        public string PrevKey { get; set; }
        public JointReflection JR;
        public List<Keyword> Keys = new List<Keyword>();
        public Dictionary<string,Keyword> Words = new Dictionary<string,Keyword>();
        public Dictionary<string, int> LastPos = new Dictionary<string, int>();
        public CodeComplete ( )
        {
            InitializeComponent ( );
           
        
        }

        private void AddType(Type t)
        {
        //    if ( Words.ContainsKey ( t.Name ) ) Words.Remove(t.Name);
           Keys.Add(new Keyword(t.Name, Color.Khaki));
        }


        public void AddProps(Type t)
        {
            foreach (var p in t.GetProperties())
            {
                var k3 = new Keyword(p.Name, Color.Empty);
                var k = new Keyword(p.Name, Color.DimGray, t.Name);
                Keys.Add(k);
                Keys.Add(k3);
                foreach (var t2 in System.Reflection.Assembly.GetAssembly(typeof (Chimp.Core.ChimpSprite)).GetTypes())
                {
                    foreach (var p2 in t2.GetProperties())
                    {
                        if (p != p2)
                        {
                            if (p2.GetType() == t)
                            {
                                var k2 = new Keyword(p.Name, Color.DimGray, p2.Name);
                                Keys.Add(k2);
                            }
                        }
                    }
                }

                //Words.Add(p.Name, new Keyword(p.Name, Color.Firebrick));
            }
        }
        public void AddFields ( Type t )
        {
            Console.WriteLine(t.Name);
            if ( t.Name.Contains ( "Chimp" ) == false ) return;
            foreach (var p in t.GetMembers())
            {
                
                //var k3 = new Keyword(p.Name, Color.Empty);
                
                var k = new Keyword(p.Name, Color.DimGray,"");
     
                Keys.Add(k);
                //Keys.Add(k3);
             
                //Words.Add(p.Name, new Keyword(p.Name, Color.Firebrick));
            }
        }
        public void UpdateReflection()
        {
            Console.WriteLine("Updating Reflection");
            KList.Clear();
            foreach(var t in JR.Root.FilterMembers("Class"))
            {
               
                foreach (var m in t.FilterMembers("Static"))
                {
                    KList.Add(new Keyword(m.Name, TagColor(m.Tags), t.Name));
                }
                foreach (var v in t.FilterMembers("Var"))
                {
                    var ot = JR.GetTypeFor(v);
                    if ( ot == null ) continue;
                    foreach(var tm in ot.Members)
                    {
                        KList.Add(new Keyword(tm.Name,TagColor(tm.Tags),v.Name));
                    }
                }
                KList.Add ( new Keyword ( t.Name, TagColor ( t.Tags ) ) );
            }
    
            foreach(var k in KList)
            {
                Console.WriteLine("CC:" + k.Prev + "." + k.Key);
            }
       
           Console.WriteLine("Keys:"+KList.Count);
        }

        public List<Keyword> KList = new List<Keyword>();
        public Color TagColor(Dictionary<string,string> tag)
        {
            return Color.Blue;
        }
        public void Update ( )
        {

            Console.WriteLine("Updating CC:");

            Keys = KList;
            List.Items.Clear();
            Console.WriteLine("Keys2:"+Keys.Count);
            Console.WriteLine("KList:"+KList.Count);
            foreach (var k in Keys)
            {
                Console.WriteLine("AK"+ActiveKey+" PK:"+PrevKey+" K:"+k.Key+" KP:"+k.Prev);
                if ( ActiveKey == k.Key && ( PrevKey == k.Prev || k.Prev == "" ) )
                {
                    Console.WriteLine("Exact Match!");
                         Close ( );
                    return;
                }
                if (k.Key.Contains(ActiveKey))
                {
                    Console.WriteLine("Key partial");
                    if (k.Prev == PrevKey || k.Prev=="")
                    {
                        Console.WriteLine("Prev match!");
                        List.Items.Add(k.Prev+"."+k.Key);
                      
                    }
                }
            }
            Console.WriteLine("Items:"+List.Items.Count);
            List.Refresh();
            if (List.Items.Count > 0)
            {
                Show();
                List.SelectedIndex = 0;
                if (LastPos.ContainsKey(ActiveKey))
                {
                        List.SelectedIndex = LastPos[ActiveKey];
                }

                Picked = false;
            }else
            {
                Close();
            }
        }

        public string PickKey { get; set; }
        public bool Picked = false;
        private void List_KeyDown ( object sender, KeyEventArgs e )
        {
            
            if(e.KeyCode==System.Windows.Forms.Keys.Return && List.SelectedIndex>=0 && List.SelectedIndex<List.Items.Count)
            {

                if(ActiveKey=="") return;

                if ( LastPos.ContainsKey ( ActiveKey ) ) LastPos.Remove(ActiveKey);
                LastPos.Add(ActiveKey, List.SelectedIndex);
                PickKey =
                    List.Items[List.SelectedIndex].ToString().Substring(
                        List.Items[List.SelectedIndex].ToString().IndexOf(".") + 1);
                Hide();
                Picked = true;
                Form1.ChangeActiveKey(PickKey);
                e.SuppressKeyPress = false;
                e.Handled = false;
                return;
            }
            if(e.KeyCode == System.Windows.Forms.Keys.Up)
            {
            //    List.SelectedIndex = List.SelectedIndex - 1;
                return;
            }
            if(e.KeyCode == System.Windows.Forms.Keys.Down && List.Items.Count>0)
            {

              //  List.SelectedIndex = List.SelectedIndex + 1;
                return;
            }
            e.SuppressKeyPress = true;
            Close();
        }
    }

}
